<?php

// @formatter:off
/**
 * @file WCParserBehaviorTRHEADER.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: WCParserBehaviorTRHEADER.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/includes/lang/WCParserBehaviorTRHEADER.php $
 */
// @formatter:on


/**
 * @class WCParserBehaviorTR This is a specific behavior that returns a variables
 * value. When it's used it looks like:
 * @code
 * <:TR:/>name<::/>
 * @endcode
 */
class WCParserBehaviorTRHEADER extends WCParserBehavior {
	/**
	 * @var WCTranslate
	 */
	protected static $_Holder = false;
	/**
	 * This method perform a check of the object, sets the current status
	 * (variable $this->_ok) and return it.
	 *
	 * @return boolean Returns the current status after a check.
	 */
	public function check() {
		//
		// A TR behavior must belong to a parser node.
		if($this->_node) {
//			//
//			// Loading node's children.
//			$children = $this->_node->children();
//			//
//			// A TR behavior requires at least one child belonging to
//			// its parser node.
//			if(!isset($children[0])) {
//				//
//				// Its parser node has no children. Error message
//				// is set and the internal status becomes false.
//				$this->_errorMsg = "node has more than one children";
//				$this->_ok = false;
//			} elseif(!is_string($children[0])) {
//				//
//				// The first child of node is not a string and it
//				// cannot be considered as a name. Error message
//				// is set and the internal status becomes false.
//				$this->_errorMsg = "node's first child is not a name";
//				$this->_ok = false;
//			} else {
			//
			// At this point, everything checks out.
			$this->_ok = true;
			$this->_errorMsg = "Ok";
//			}
		} else {
			//
			// Behavior's node is not present. Error message is set
			// and the internal status becomes false.
			$this->_errorMsg = "TR node is not set";
			$this->_ok = false;
		}
		//
		// Returning current object status.
		return $this->isOk();
	}
	/**
	 * This method actually performs the function represented based on its
	 * node's data.
	 *
	 * @return string Returns the value of a variable using node's contents as
	 * a name. Otherwise, returns false.
	 */
	public function get() {
		//
		// Setting the default value to be returned.
		$out = "";
		//
		// Loading parameters.
		$params = $this->_node->params();

		global $wcPaths;
		global $wcROOTURI;

		$file = "{$wcROOTURI}/{$wcPaths["includes-directory-uri"]}/lang/wcomix.lang.js";

		$out = "<script type=\"text/javascript\" src=\"{$file}\">\n";
		$out.= "\tconsole.log(\"Unable to load '{$file}'\");\n";
		$out.= "</script>\n";




		$file = "{$wcROOTURI}/{$wcPaths["cache-directory-uri"]}/{$wcPaths["cache-js-uri"]}/".WCTranslate::JSPrefix;
		$file.=WCTranslate::I()->lang();
		$file.=".js";

		$out.= "<script type=\"text/javascript\" src=\"{$file}\">\n";
		$out.= "\tconsole.log(\"Unable to load '{$file}'\");\n";
		$out.= "</script>\n";
		//
		// When there's a parameter 'padding' set, every line
		// must be indented with as many TAB characters as the
		// number given by the parameter 'padding'.
		if(isset($params["padding"])) {
			//
			// General spacer.
			$padding = str_repeat("\t", $params["padding"]);
			//
			// When the parameter 'nfl' is
			// given, the first line is not
			// indented.
			$out = (isset($params["nfl"]) ? "" : $padding).str_replace("\n", "\n".$padding, $out);
		}
		//
		// Returning evaluation results.
		return $out;
	}
	//
	// Protected methods.
	protected function value() {
		$children = $this->_node->children();
		return "{$children[0]}";
	}
}
